using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Collections;
using BTV.TVAnyTime;

namespace BTV.Base.Printing
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased")]
    public class XMLPrinter : BTVPrinter
    {
        public XMLPrinter(List<BTVChannel> input) : base(input) { }

        public override void ProduceReport()
        {
            DataStream = new MemoryStream();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "    ";
            using (XmlWriter writer = XmlWriter.Create(DataStream, settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("tv_guide");
                foreach (BTVChannel channel in Channels)
                {
                    writer.WriteStartElement("channel");
                    writer.WriteAttributeString("name", channel.Name);
                    Dictionary<BTVSchedule, BTVProgram> filler = GetFiller(channel, 0);
                    foreach (KeyValuePair<BTVSchedule, BTVProgram> fkvp in filler)
                    {
                        BTVProgram fProg = fkvp.Value;
                        BTVSchedule fSched = fkvp.Key;
                        writer.WriteStartElement("program");
                        writer.WriteElementString("name", fProg.Name);
                        writer.WriteElementString("url", fProg.PID);
                        string sMin = fSched.StartTime.Minute.ToString();
                        if (sMin.Length != 2)
                            sMin = "0" + sMin;
                        writer.WriteElementString("startTime", fSched.StartTime.Hour + ":" + sMin);
                        writer.WriteStartElement("duration");
                            writer.WriteValue(fSched.Duration.TotalMinutes);
                        writer.WriteEndElement();
                        writer.WriteElementString("description", fProg.GetShortSynopsis());
                        writer.WriteEndElement();
                    }
                    foreach (KeyValuePair<string, List<BTVSchedule>> kvp in channel.Schedules)
                    {
                        foreach (BTVSchedule sched in kvp.Value)
                        {
                            BTVProgram prog = new BTVProgram();
                            if (channel.Programs.TryGetValue(sched.Pid, out prog))
                            {
                                writer.WriteStartElement("program");
                                    writer.WriteElementString("name", prog.Name);
                                    writer.WriteElementString("url", prog.PID);
                                    string sMin = sched.StartTime.Minute.ToString();
                                    if (sMin.Length != 2)
                                        sMin = "0" + sMin;
                                    writer.WriteElementString("startTime", sched.StartTime.Hour + ":" + sMin);
                                    writer.WriteElementString("startTimeInteger", ""+sched.StartTime.Hour + sMin);
                                    writer.WriteStartElement("duration");
                                        writer.WriteValue(sched.Duration.TotalMinutes);
                                    writer.WriteEndElement();
                                    writer.WriteElementString("description", prog.GetShortSynopsis());
                                writer.WriteEndElement();
                            }
                        }
                    }
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
            }
        }
    }
}
